package me.exchange.com.modules.system.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import me.exchange.com.aop.log.Log;
import me.exchange.com.modules.system.dao.QiniuContentMapper;
import me.exchange.com.modules.system.domain.Scroll;
import me.exchange.com.modules.system.dto.ScrollDto;
import me.exchange.com.modules.system.dto.ScrollQueryCriteria;
import me.exchange.com.modules.system.entity.QiniuContent;
import me.exchange.com.modules.system.service.ScrollService;
import me.exchange.com.utils.Result;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SimpleTimeZone;

/**
 * @author zhaojing
 * @create2020/5/23
 */
@Api(tags = "图片管理")
@RequestMapping("/api/scroll")
@RestController
public class ScrollController {

    @Autowired
    private ScrollService scrollService;

    @PostMapping("/queryInfo")
    @Log("查询详情")
    @ApiOperation("图片查询详情")
    @PreAuthorize("@el.check('scroll:list')")
    public ResponseEntity<Object> queryInfo(Long id){

        ScrollDto scrollDto = scrollService.findById(id);

        return new ResponseEntity<Object>(Result.ok("图片管理详情查询成功").put("scroll",scrollDto), HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log("新添信息")
    @ApiOperation("新添信息")
    @PreAuthorize("@el.check('scroll:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Scroll resources){

        scrollService.create(resources);

        return new ResponseEntity<Object>(Result.ok("滚筒图片管理添加成功"),HttpStatus.OK);

    }

    @PostMapping("/update")
    @Log("编辑信息")
    @ApiOperation("编辑信息")
    @PreAuthorize("@el.check('scroll:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Scroll resources){

        scrollService.update(resources);

        return new ResponseEntity<Object>(Result.ok("滚动图片管理修改成功"),HttpStatus.OK);
    }

    @PostMapping("/delete")
    @Log("删除信息")
    @ApiOperation("删除信息")
    @PreAuthorize("@el.check('scroll:delete')")
    public ResponseEntity<Object> delete(@Validated @RequestBody Scroll resources){

        scrollService.delete(resources.getId());

        return new ResponseEntity<Object>(Result.ok("滚动图片管理删除成功"),HttpStatus.OK);
    }

    @PostMapping("/deleteAll")
    @Log("多选删除")
    @ApiOperation("多选删除")
    @PreAuthorize("@el.check('scroll:deleteAll')")
    public ResponseEntity<Object> deleteAll(@RequestBody ScrollQueryCriteria criteria){
        scrollService.deleteAll(criteria.getIds());
        return new ResponseEntity<Object>(Result.ok("多选删除成功"),HttpStatus.OK);
    }

    @PostMapping("/list/download")
    @Log("导出数据")
    @ApiOperation("导出数据")
    @PreAuthorize("@el.check('scroll:list')")
    public void downLoad(HttpServletResponse response, ScrollQueryCriteria criteria) throws Exception {

        scrollService.downLoad(scrollService.queryAll(criteria),response);

    }

    @PostMapping("/list")
    @Log("详情查询")
    @ApiOperation("详情查询")
    @PreAuthorize("@el.check('scroll:list')")
    public ResponseEntity<Object> queryAll(ScrollQueryCriteria criteria){

        List<ScrollDto> scrollDtos = scrollService.queryAll(criteria);

        return new ResponseEntity<Object>(Result.ok("详情查询成功").put("Scroll",scrollDtos),HttpStatus.OK);
    }

    @PostMapping("/updateEnable")
    @Log("状态修改")
    @ApiOperation("状态修改")
    @PreAuthorize("@el.check('scrill:edit')")
    public ResponseEntity<Object> updateEnable(@RequestBody ScrollQueryCriteria criteria){

        scrollService.updateEnable(criteria);

        return new ResponseEntity<Object>(Result.ok("状态修改成功"),HttpStatus.OK);
    }

    @PostMapping("/upload")
    @Log("上传")
    @ApiOperation("上传")
    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> upload(@RequestBody Scroll scroll, MultipartFile file) throws  Exception{

        Map<String, Object> map = new LinkedMap<>();

        if (file == null){
            map.put("code","0001");
            map.put("message","图片上传失败");
        }else {
            String str = "D:/";//暂时存放地址
            String fileName = file.getOriginalFilename();//获取图片名称
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            String nowTime = simpleDateFormat.format(date);
            fileName = nowTime + fileName.substring(fileName.lastIndexOf("."));
            String suffxxName = fileName.substring(fileName.lastIndexOf("."));

            if (!suffxxName.equals(".jpg") && !suffxxName.equals(".png")) {
                map.put("code","9999");
                map.put("message","请选择.jpg 或者 .png格式的文件");
                return (ResponseEntity<Object>) map;
            }

            File saveFile = new File( str + fileName);

            if (!saveFile.getParentFile().exists()){
                saveFile.getParentFile().mkdirs();
            }

            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
            out.write(file.getBytes());
            out.flush();
            out.close();

            scroll.setAdress(String.valueOf(saveFile));
            scroll.setScrollName(fileName);

            QiniuContent qiniuContent = new QiniuContent();

            qiniuContent.setUrl(scroll.getAdress());
            map.put("code","0000");
            map.put("nessage","上传成功");
        }
        return new ResponseEntity<>(Result.ok("上传成功").mergeMap(map),HttpStatus.OK);
    }

}
